package com.bawei.module_home.ui

import android.util.Log
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseFragment
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.module_home.R
import com.bawei.module_home.adapter.HomeNoticeAdapter
import com.bawei.module_home.adapter.HomeServiceAdapter
import com.bawei.module_home.databinding.FragmentHomeBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bawei.module_home.model.HomeServiceModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class HomeFragment : BaseFragment<BaseViewModel, FragmentHomeBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.fragment_home
    private val mViewModel: HomeServiceViewModel by viewModels()

    val serviceAdapter: HomeServiceAdapter by lazy {
        HomeServiceAdapter().apply {
            setOnItemClickListener(object : HomeServiceAdapter.OnItemClickListener {
                override fun onItemClick(item: HomeServiceModel, position: Int) {
                    when (position) {
                        0 -> {
                            //车位管理
                            ARouter.getInstance().build(Router.AREA_PAGE).navigation()
                        }

                        1 -> {
                            //访客登记
                            ARouter.getInstance().build(Router.VISITOR_PAGE).navigation()
                        }

                        2 -> {
                            //报修管理
                            ARouter.getInstance().build(Router.REPAIR_PAGE).navigation()

                        }

                        3 -> {
                            //考勤打卡
                            ARouter.getInstance().build(Router.SIGN_PAGE).navigation()

                        }

                        4 -> {
                            //房源
                            ARouter.getInstance().build(Router.HOUSE_PAGE).navigation()

                        }

                        5 -> {
                            //商家推荐
                            ARouter.getInstance().build(Router.PROFILES_PAGE).navigation()
                        }

                        6 -> {
                            //会员卡

                            ARouter.getInstance().build(Router.VIP_PAGE).navigation()

                        }

                        7 -> {
                            //设备管理
                            ARouter.getInstance().build(Router.DEVICES_PAGE).navigation()
                        }
                    }
                }
            })
        }
    }

    val noticeAdapter: HomeNoticeAdapter by lazy {
        HomeNoticeAdapter()
    }

    override fun initView() {
        super.initView()
        mViewModel.initList()
        initAdapter()

    }

    //获取数据
    override fun initData() {
        mViewModel.getNotice(1, 20)
    }

    //实现网络接口
    override fun initListener() {
        mViewModel.noticeState.observeWithLifecycle(this) {
            when (it) {
                Empty -> {}
                is Failed -> {
                    Log.i("GZY", "initListener: " + it.message)
                }

                Loading -> {}
                Success -> {
                    noticeAdapter.setData(mViewModel.noticeList)
                    Log.i("GZY", "initListener: " + mViewModel.noticeList.size)
                }
            }
        }
    }

    private fun initAdapter() {
        serviceAdapter.setData(mViewModel.ServiceList)
        mBinding.rv1.layoutManager = GridLayoutManager(context, 4)
        mBinding.rv1.adapter = serviceAdapter


        mBinding.rv2.layoutManager = LinearLayoutManager(context)
        mBinding.rv2.adapter = noticeAdapter


    }

}